<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-form ref="form" :model="queryInfo" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="商品名称：">
              <el-input @clear="getGoodsList" v-model="queryInfo.query" placeholder="请输入商品名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row style="float: right">
          <el-button type="primary" size="small" @click="getGoodsList">查 询</el-button>
          <el-button type="warning" size="small" @click="resetQueryInfo">重 置</el-button>
        </el-row>
      </el-form>

      <el-button type="primary" size="small" style="margin-top: 10px" @click="goAddPage">添加商品</el-button>

      <el-table :data="goodsList" border stripe fit>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="200px" align="center"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="120px" align="center"></el-table-column>
        <el-table-column label="商品数量" prop="goods_number" width="120px" align="center"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="120px" align="center"></el-table-column>
        <el-table-column label="商品状态" prop="goods_state" width="100px" align="center">
          <template slot-scope="scope">
            {{ Number(scope.row.goods_state) === 0 ? '未通过' : '' }}
            {{ Number(scope.row.goods_state) === 1 ? '审核中' : '' }}
            {{ Number(scope.row.goods_state) === 2 ? '已审核' : '' }}
          </template>
        </el-table-column>
        <el-table-column label="热销商品" prop="is_promote" width="100px" align="center">
          <template slot-scope="scope">
            {{ scope.row.is_promote ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column label="热销商品数量" prop="hot_mumber" width="120px" align="center"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="160px" align="center">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="更新时间" prop="upd_time" width="160px" align="center">
          <template slot-scope="scope">
            {{ scope.row.upd_time | dateFormat }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="130px" align="center">
          <template slot-scope="scope">
            <el-button
              @click="showEditDialog(scope.row.goods_id)"
              size="mini"
              type="primary"
              icon="el-icon-edit"
            ></el-button>

            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="removeById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 修改商品信息对话框 -->
    <el-dialog @close="editDialogClosed" title="修改商品信息" :visible.sync="editDialogVisible" width="50%">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="180px">
        <el-form-item label="商品名称：">
          <el-input v-model="editForm.goods_name" disabled></el-input>
        </el-form-item>

        <el-form-item label="价格：" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>

        <el-form-item label="数量：" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>

        <el-form-item label="重量：" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>

        <el-form-item label="商品状态：" prop="goods_state">
          <el-select v-model="editForm.goods_state" placeholder="请选择商品状态">
            <el-option
              v-for="item in goods_state"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否为热销商品：" prop="is_promote">
          <el-select v-model="editForm.is_promote" placeholder="请选择是否热销">
            <el-option label="是" :value="true"></el-option>
            <el-option label="否" :value="false"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-if="editForm.is_promote" label="热销商品数量：" prop="hot_mumber">
          <el-input v-model="editForm.hot_mumber"></el-input>
        </el-form-item>

        <el-form-item label="介绍：" prop="goods_introduce">
          <quill-editor v-model="editForm.goods_introduce"></quill-editor>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodsInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getGoodsList, delGoodsById, getGoodsInfoById, putEditGoodsInfo } from '@/api/goods'

export default {
  name: 'goodsList',
  data() {
    return {
      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 商品列表
      goodsList: [],
      // 总数据条数
      total: 0,
      // 控制修改商品信息对话框的展示和隐藏
      editDialogVisible: false,
      // 查询到的商品信息对象
      editForm: {},
      editFormRules: {
        goods_price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }],
        goods_number: [{ required: true, message: '请输入商品数量', trigger: 'blur' }]
      },
      goods_state: [
        { value: 0, label: '未通过' },
        { value: 1, label: '审核中' },
        { value: 2, label: '已审核' }
      ]
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 根据分页获取商品列表
    async getGoodsList() {
      const res = await getGoodsList(this.queryInfo)
      if (res.meta.status === 200) {
        this.goodsList = res.data.goods
        this.total = res.data.total
      } else {
        this.$message.error(res.meta.msg)
      }
    },

    // 重置查询条件
    async resetQueryInfo() {
      this.queryInfo.query = ''
      const res = await getGoodsList(this.queryInfo)
      if (res.meta.status === 200) {
        this.goodsList = res.data.goods
        this.total = res.data.total
      } else {
        this.$message.error(res.meta.msg)
      }
    },

    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },

    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },

    // 点击按钮跳转到添加商品页面
    goAddPage() {
      this.$router.push('/goods/add')
    },

    // 展示编辑商品信息的对话框
    async showEditDialog(id) {
      const res = await getGoodsInfoById(id)
      if (res.meta.status === 200) {
        this.editForm = res.data
        this.editDialogVisible = true
      } else {
        this.$message.error(res.meta.msg)
      }
    },

    // 修改商品信息并提交
    editGoodsInfo() {
      this.$refs.editFormRef.validate(async valid => {
        if (valid) {
          let params = this.editForm
          const res = await putEditGoodsInfo(params)
          if (res.meta.status === 200) {
            this.editDialogVisible = false
            this.getGoodsList()
            this.$message.success(res.meta.msg)
          } else {
            return this.$message.error(res.meta.msg)
          }
        }
      })
    },

    // 监听修改商品信息对话框关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },

    // 删除商品
    async removeById(id) {
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult === 'confirm') {
        const res = await delGoodsById(id)
        if (res.meta.status === 200) {
          this.$message.success(res.meta.msg)
          this.getGoodsList()
        } else {
          this.$message.error(res.meta.msg)
        }
      } else {
        this.$message.info('取消了删除')
      }
    }
  }
}
</script>

<style scoped></style>
